package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: anubhav
 * Date: 1/7/14
 * Time: 12:28 AM
 * To change this template use File | Settings | File Templates.
 */
public class Login implements Action {

    private PrintStream out;
    private InputSeeker in;
    private Map<String, User> userList;
    private UserMenuComposer menuComposer;

    public Login(PrintStream out, InputSeeker in, Map<String, User> userList, UserMenuComposer menuComposer) {
        this.out = out;
        this.in = in;
        this.userList = userList;
        this.menuComposer = menuComposer;
    }

    @Override
    public void executeAction() {

        out.print("Enter your library number: ");
        String libNum = in.takeStringInput();
        if (LibraryNumberValidator.validate(libNum)) {
            out.print("Enter password: ");
            String password = in.takeStringInput();
            UserAuthenticator authenticator = new UserAuthenticator(userList);
            User user = authenticator.authenticate(libNum, password);
            if (user != null) {
                MenuGenerator userMenu = menuComposer.getUserMenu(user);
                userMenu.generateMenu();
                new InputParser(in, userMenu, out).parse();
            } else {
                out.println("Wrong password!!");
                executeAction();
            }


        } else {
            out.println("Invalid Library Number!!");
            executeAction();
        }

    }
}
